import React from 'react';
import { Button } from 'antd';
import { WeaTools, WeaLocaleProvider, WeaTop, WeaDialog, WeaSearchGroup, WeaFormItem, WeaInput } from 'ecCom';
import WeaLicenseSubmit from '../wea-license-submit';
import WeaNonStandard from '../wea-non-standard';

const getLabel = WeaLocaleProvider.getLabel;

class WeaLicense extends React.Component {
  constructor(props) {
    super(props);
    this.state = { loading: false, data: {}, nsusecnt: 0 };
  }

  componentWillMount() {
    const { display, visible } = this.props;
    if (display == 'page' || visible) {
      this.onLoad();
    }
  }

  componentWillReceiveProps(nextProps) {
    const { visible } = nextProps;
    if (visible) {
      this.onLoad();
    }
  }

  render() {
    const { display, visible } = this.props;
    const { loading, data = {}, nsusecnt } = this.state;
    const { canEdit, companyName, licenseCode, hrmNum, scCount, expireDate, concurrentFlag, onlineusercount, licensenum, unusedlice } = data;

    const colProps = { labelCol: { span: 8 }, wrapperCol: { span: 16 } };

    const Content = (
      <React.Fragment ecId={`${this && this.props && this.props.ecId || ''}_React.Fragment@xn8kqn`}>
        <WeaLicenseSubmit ecId={`${this && this.props && this.props.ecId || ''}_WeaLicenseSubmit@3clzxv`} ref={lsRef => (this.lsRef = lsRef)} />
        <WeaNonStandard ecId={`${this && this.props && this.props.ecId || ''}_WeaNonStandard@eijwdq`} ref={nsRef => (this.nsRef = nsRef)} />
        <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@ybbgfo`} title={getLabel(1361, '基本信息')} showGroup={true} center>
          <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@26sfh2`} label={getLabel(16898, '授权用户')} {...colProps} underline>
            <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@syumsu`} value={companyName} viewAttr={1} hasBorder={false} />
          </WeaFormItem>
          <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@b8c5qy`} label={getLabel(18639, '标识码')} {...colProps} underline>
            <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@1xge8r`} value={licenseCode} viewAttr={1} hasBorder={false} />
          </WeaFormItem>
          <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@ckqbwa`} label={getLabel(15029, '用户数')} {...colProps} underline>
            <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@dxsciq`} value={hrmNum} viewAttr={1} hasBorder={false} />
          </WeaFormItem>
          <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@9723z2`} label={getLabel(82496, '分部数')} {...colProps} underline>
            <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@w86m2a`} value={scCount} viewAttr={1} hasBorder={false} />
          </WeaFormItem>
          <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@zp1jcl`} label={getLabel(741, '结束日期')} {...colProps} underline>
            <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@2dj5ty`} value={expireDate} viewAttr={1} hasBorder={false} />
          </WeaFormItem>
        </WeaSearchGroup>
        <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@4tvnkr`} title={getLabel(21519, 'license使用情况')} showGroup={true} center>
          <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@rz9tev`} label={getLabel(21522, '已使用数量')} {...colProps} underline>
            <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@g0tywf`} value={concurrentFlag == '1' ? onlineusercount : licensenum} viewAttr={1} hasBorder={false} />
          </WeaFormItem>
          <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@cbp4nf`} label={getLabel(21523, '未使用数量')} {...colProps} underline>
            <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@7esn2h`} value={unusedlice} viewAttr={1} hasBorder={false} />
          </WeaFormItem>
        </WeaSearchGroup>
        <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@gwk7tj`} title={getLabel(517206, '非标功能启用情况')} showGroup={true} center>
          <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@fn3pva`} label={getLabel(517207, '已启用数量')} {...colProps} underline>
            {canEdit ? (
              <div className="wea-field-readonly">
                <a href="javascript:;" className="child-item" onClick={() => this.nsRef.onShow()}>
                  {nsusecnt}
                </a>
              </div>
            ) : (
              <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@1sj8rl`} value={nsusecnt} viewAttr={1} hasBorder={false} />
            )}
          </WeaFormItem>
        </WeaSearchGroup>
      </React.Fragment>
    );

    if (display == 'page') {
      return (
        <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@7ynjal`}
          loading={loading}
          title={getLabel(18014, '授权信息')}
          icon={<i className="icon-portal-license" />}
          iconBgcolor="#a7adb5"
          buttons={this.getButtons()}
        >
          {Content}
        </WeaTop>
      );
    }

    return (
      <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@9am8wz`}
        visible={visible}
        loading={loading}
        title={getLabel(18014, '授权信息')}
        icon="icon-portal-license"
        iconBgcolor="#a7adb5"
        style={{ width: 700, height: 563 }}
        zIndex={100}
        hasScroll={true}
        buttons={this.getButtons()}
        onCancel={this.onCancel}
      >
        {Content}
      </WeaDialog>
    );
  }

  getButtons = () => {
    const { data = {} } = this.state;
    const buttons = [];
    if (data.canEdit) {
      buttons.push(
        <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@vt3ogn`} type="primary" onClick={() => this.lsRef.onShow()}>
          {getLabel(18640, '提交license')}
        </Button>,
      );
    }
    return buttons;
  };

  onLoad = () => {
    this.setState({ loading: true });
    WeaTools.callApi('/api/system/license/licenseInfo', 'POST').then((result) => {
      this.setState({ loading: false, data: result });
    });

    WeaTools.callApi('/api/portal/unStandardFuncInfo/count', 'POST').then((result) => {
      this.setState({ nsusecnt: result.onCount });
    });
  };

  onCancel = () => {
    this.props.onHide();
  };
}

export default WeaLicense;
